package com.huacai.homestay.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.huacai.common.core.domain.entity.SysUser;
import com.huacai.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.huacai.common.annotation.Log;
import com.huacai.common.core.controller.BaseController;
import com.huacai.common.core.domain.AjaxResult;
import com.huacai.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.huacai.homestay.domain.Homestay;
import com.huacai.homestay.service.IHomestayService;
import com.huacai.common.utils.poi.ExcelUtil;
import com.huacai.common.core.page.TableDataInfo;

/**
 * 酒店Controller
 *
 * @author hsj
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/huacai/homestay")
public class HomestayController extends BaseController {
    @Autowired
    private IHomestayService homestayService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询酒店列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Homestay homestay) {
        startPage();
        List<Homestay> list = homestayService.selectHomestayList(homestay);
        return getDataTable(list);
    }

    /**
     * 导出酒店列表
     */
    @Log(title = "酒店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Homestay homestay) {
        List<Homestay> list = homestayService.selectHomestayList(homestay);
        ExcelUtil<Homestay> util = new ExcelUtil<Homestay>(Homestay.class);
        util.exportExcel(response, list, "酒店数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Homestay> util = new ExcelUtil<Homestay>(Homestay.class);
        util.importTemplateExcel(response, "酒店数据");
    }

    /**
     * 导入数据
     */
    @PreAuthorize("@ss.hasPermi('huacai:homestay:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Homestay> util = new ExcelUtil<Homestay>(Homestay.class);
        InputStream inputStream = file.getInputStream();
        List<Homestay> list = util.importExcel(inputStream);
        inputStream.close();
        int count = homestayService.batchInsertHomestay(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取酒店详细信息
     */
    @GetMapping(value = "/{homestayId}")
    public AjaxResult getInfo(@PathVariable("homestayId") String homestayId) {
        return success(homestayService.selectHomestayByHomestayId(homestayId));
    }

    /**
     * 新增酒店
     */
    @Log(title = "酒店", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Homestay homestay) {

        return toAjax(homestayService.insertHomestay(homestay));
    }

    /**
     * 修改酒店
     */
    @Log(title = "酒店", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Homestay homestay) {
        return toAjax(homestayService.updateHomestay(homestay));
    }

    /**
     * 删除酒店
     */
    @Log(title = "酒店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{homestayIds}")
    public AjaxResult remove(@PathVariable String[] homestayIds) {
        return toAjax(homestayService.deleteHomestayByHomestayIds(homestayIds));
    }

    /**
     * 通过景点ID查询附近酒店列表
     */
    @GetMapping(value = "/selectHomestayBySpotsId/{spotsId}")
    public AjaxResult selectHomestayBySpotsId(@PathVariable("spotsId") String spotsId) {
        List<Homestay> list = homestayService.selectHomestayBySpotsId(spotsId);
        return success(list);
    }

    /**
     * 通过餐厅ID查询附近酒店列表
     */
    @GetMapping(value = "/selectHomestayByRestaurantId/{restaurantId}")
    public AjaxResult selectHomestayByRestaurantId(@PathVariable("restaurantId") Integer restaurantId) {
        List<Homestay> list = homestayService.selectHomestayByRestaurantId(restaurantId);
        for (Homestay homestay : list) {
            System.out.println(homestay);
        }
        return success(list);
    }

    /**
     * 查询特色酒店列表和酒店标签
     */
    @PostMapping("/selectHomestayOrTagsList")
    public AjaxResult selectHomestayOrTagsList(@RequestBody Homestay homestay) {
        List<Homestay> list = homestayService.selectHomestayOrTagsList(homestay);
        return success(list);
    }

    /**
     * 账户充值
     */
    @PutMapping(value = "/updateUserAmount/{amount}")
    public AjaxResult updateUserAmount(@PathVariable("amount") double amount) {
        long userId = getUserId();//当前充值用户ID
        Double balance= sysUserService.selectUserById(userId).getBalance(); //当前充值账户余额
        SysUser user= new SysUser();
        user.setUserId(userId);
        user.setBalance(balance+amount);
        sysUserService.updateUser(user);
        Double newBalance = user.getBalance();//充值后的金额
//        System.out.println("充值成功，当前余额为："+newBalance);
        return success(newBalance);
    }


}
